package com.bnlr.lrparking.appui.carplace;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bnlr.lrparking.appui.stall.MyStallDetailsActivity;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Urls;
import com.flyco.animation.BounceEnter.BounceTopEnter;
import com.flyco.animation.SlideExit.SlideBottomExit;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.mcxtzhang.swipemenulib.SwipeMenuLayout;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.model.StallListM;
import com.bnlr.lrparking.view.RecyclerItemClickListener;
import com.bnlr.lrparking.util.CommonUtil;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/6/7.
 * 车位
 */
public class CarPlaceActivity extends BaseActivity {

    @BindView(R.id.tv_nav_title)
    TextView tvNavTitle;
    @BindView(R.id.tv_nav_right)
    TextView tvNavRight;
    @BindView(R.id.iv_nav_right)
    ImageView ivNavRight;
    @BindView(R.id.iv_nav_back)
    ImageView ivNavBack;
    @BindView(R.id.lv_mycar_list)
    RecyclerView lvMycarList;
    @BindView(R.id.iv_hint)
    ImageView ivHint;
    @BindView(R.id.tv_hint_tips)
    TextView tvHintTips;
    @BindView(R.id.ll_hint)
    LinearLayout llHint;
    private MyCarAdapter adapter;
    private List<StallListM.DataBean> parkList = new ArrayList<>();
    private StallListM.DataBean bean = new StallListM.DataBean();
    private ImageView iv_nav_right;
    private TextView tv_go_addstall, tv_go_invest; //去添加，去投资


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stall);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        iv_nav_right = (ImageView) findViewById(R.id.iv_nav_right);
        tv_go_addstall = (TextView) findViewById(R.id.tv_go_addstall);
        tv_go_invest = (TextView) findViewById(R.id.tv_go_invest);
        iv_nav_right.setImageResource(R.mipmap.right_icon_add);
        tvNavTitle.setText("我的车位");
        linearLayoutManager = new LinearLayoutManager(baseContext);
        lvMycarList.setLayoutManager(linearLayoutManager);
        tv_go_addstall.setOnClickListener(this);
        tv_go_invest.setOnClickListener(this);
        iv_nav_right.setOnClickListener(this);
        lvMycarList.setItemAnimator(new DefaultItemAnimator());
        lvMycarList.addOnItemTouchListener(
                new RecyclerItemClickListener(baseContext, lvMycarList, new RecyclerItemClickListener.OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        // do whatever
                        Intent intent = new Intent(CarPlaceActivity.this, MyStallDetailsActivity.class);
                        intent.putExtra("place_detail", parkList.get(position));
                        startActivity(intent);
                    }

                    @Override
                    public void onLongItemClick(View view, int position) {
                        // do whatever
                    }
                })
        );
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_nav_right:
                startActivity(AddStallActivity.class);
                break;
            case R.id.tv_go_addstall:
                startActivity(AddStallActivity.class);
                break;
            case R.id.tv_go_invest://去投资
//                startActivity(AddStallActivity.class);
                finish();
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        getStallList();
    }

    public class MyCarAdapter extends CommonAdapter<StallListM.DataBean> {

        public MyCarAdapter(Context context, int layoutId, List<StallListM.DataBean> datas) {
            super(context, layoutId, datas);
        }

        @Override
        protected void convert(ViewHolder holder, final StallListM.DataBean s, int position) {
            holder.setText(R.id.carplace_number_num, s.getNumber());
            SwipeMenuLayout swipe = holder.getView(R.id.SwipeMenuLayout);
            Button stall_btnDel = holder.getView(R.id.stall_btnDel);
            if (s.getStatus().equals("0")) {
                holder.setText(R.id.stall_check, "已审核");
                holder.setTextColorRes(R.id.stall_check, R.color.main_color);
                holder.setImageResource(R.id.img_stall_check, R.mipmap.icon_me_check3);
                swipe.setSwipeEnable(false);
            } else if (s.getStatus().equals("2")) {
                holder.setText(R.id.stall_check, "审核中");
                holder.setTextColorRes(R.id.stall_check, R.color.gray03_color);
                holder.setImageResource(R.id.img_stall_check, R.mipmap.icon_me_check1);
                swipe.setSwipeEnable(false);
            } else if (s.getStatus().equals("3")) {
                holder.setText(R.id.stall_check, "审核失败");
                holder.setTextColorRes(R.id.stall_check, R.color.order_type);
                holder.setImageResource(R.id.img_stall_check, R.mipmap.icon_me_check2);
            }
            stall_btnDel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final NormalDialog dialog = new NormalDialog(baseContext);
                    dialog.title("提示")
                            .titleTextColor(baseContext.getResources().getColor(R.color.black_color))
                            .titleTextSize(15)
                            .content("是否删除此车位？")
                            .contentTextSize(15)
                            .contentTextColor(baseContext.getResources().getColor(R.color.gray_color))
                            .btnText("取消", "确定")
                            .btnNum(2)
                            .style(NormalDialog.STYLE_TWO)
                            .btnTextColor(baseContext.getResources().getColor(R.color.gray_color), baseContext.getResources().getColor(R.color.colorAccent))
                            .btnTextSize(15, 15)
                            .widthScale(0.8f)//
                            .showAnim(new BounceTopEnter())//
                            .dismissAnim(new SlideBottomExit())//
                            .cornerRadius(7)
                            .show();
                    dialog.setCancelable(true);
                    dialog.setOnBtnClickL(
                            new OnBtnClickL() {
                                @Override
                                public void onBtnClick() {
                                    dialog.dismiss();
                                }
                            },
                            new OnBtnClickL() {
                                @Override
                                public void onBtnClick() {
                                    dialog.dismiss();
                                    getDeleteStall(s.getId());
                                }
                            });
                }
            });
        }
    }

    //车位列表请求
    private void getStallList() {
        try {
            mRequest = NoHttp.createStringRequest(Urls.IP, LrConstant.POST);
            mRequest.add("action", "GetUserPark");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<StallListM>(baseContext, true, StallListM.class) {
                        @Override
                        public void doWork(StallListM data, String code) {
                            if (data.getMsgcode().equals("1")) {
                                if (data != null && data.getData().size() != 0) {
                                    llHint.setVisibility(View.GONE);
                                    parkList.clear();
                                    parkList.addAll(data.getData());
                                    adapter = new MyCarAdapter(baseContext, R.layout.item_park, parkList);
                                    lvMycarList.setAdapter(adapter);
                                }
                            }
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                if (obj.getString("msgcode").equals("0")) {
                                    llHint.setVisibility(View.VISIBLE);
                                    tv_go_addstall.setVisibility(View.VISIBLE);
                                    tv_go_invest.setVisibility(View.VISIBLE);
                                    ivHint.setBackgroundResource(R.mipmap.noparkingspace);
                                    tvHintTips.setText("暂无车位信息哦~");
                                    parkList.clear();
                                    adapter = new MyCarAdapter(baseContext, R.layout.item_park, parkList);
                                    lvMycarList.setAdapter(adapter);
                                    adapter.notifyDataSetChanged();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }, true);
        } catch (Exception e) {

        }
    }

    //删除车位
    private void getDeleteStall(String id) {
        try {
            mRequest = NoHttp.createStringRequest(Urls.SPACE_IP, LrConstant.POST);
            mRequest.add("action", "DeleteSpace");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("id", id);

            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<StallListM>(baseContext, true, StallListM.class) {
                        @Override
                        public void doWork(StallListM data, String code) {
                            if (code.equals("1")) {
                                getStallList();
                                adapter.notifyDataSetChanged();
                            } else {
                                CommonUtil.showToask(CarPlaceActivity.this, data.getMsg());
                            }
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            try {
                                super.onFinally(obj, code, isSucceed);
                                if (code.equals("1")) {
                                } else {
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, false);
        } catch (Exception e) {

        }
    }
}
